package com.android.launcher3.widget;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.launcher3.R;
import com.android.launcher3.Utilities;

public class SettingItemWidget extends FrameLayout {

    private final String TAG = "SettingItemWidget";

    protected Context mContext = null;

    protected ImageView icon = null;
    protected TextView mTvName = null;
    protected TextView mTvSecName = null;
    private View container;

    public SettingItemWidget(@NonNull Context context) {
        super(context);
        this.mContext = context;
        init(null);
    }

    public SettingItemWidget(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init(attrs);
    }

    public SettingItemWidget(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        init(attrs);
    }

    ///子类重新去实现逻辑
    void setUpView() {

    }


    void init(@Nullable AttributeSet attrs) {
        LayoutInflater.from(getContext()).inflate(R.layout.layout_setting_item, this, true);
        container = findViewById(R.id.container);
        if (attrs != null) {
            TypedArray a = getContext().getTheme().obtainStyledAttributes(
                    attrs,
                    R.styleable.SettingItemWidget,
                    0, 0);

            try {
                Drawable drawable = a.getDrawable(R.styleable.SettingItemWidget_settingImage);
                String text = a.getString(R.styleable.SettingItemWidget_settingText);
                icon = findViewById(R.id.icon);
                mTvName = findViewById(R.id.tv_name);
                mTvSecName = findViewById(R.id.tv_sec_name);
                if (drawable != null) {
                    icon.setImageDrawable(drawable);
                }

                if (text != null) {
                    mTvName.setText(text);
                }

                boolean showSec = a.getBoolean(R.styleable.SettingItemWidget_showSec, true);

                if (!showSec) {
                    mTvSecName.setVisibility(GONE);
                }

            } finally {
                a.recycle();
            }

        }

        setUpView();

        screenAdaption();
    }

    private void screenAdaption() {

        if (Utilities.isLongStripScreen(getContext()) || getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            container.setBackgroundResource(R.drawable.bg_round_corner_selector_small);
        }

    }
}
